package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.user.SysUserServiceImpl;
import com.alibaba.algo.controller.out.UserInfoResVO;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.*;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.UserResourceInfoVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * 类SysUserController.java的实现描述：用户表 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysUser")
@Api(value = "用户管理", tags = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserService;

    @PostMapping("/insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody SaveUserRequestVO vo) {
        if (StringUtils.isEmpty(vo.getPasswd())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_PASSWD_ERROR.getCode(), GlobalResultStatus.PARAM_USER_PASSWD_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getUserName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_USER_NAME_ERROR.getMsg());
        }
        if (CollectionUtils.isEmpty(vo.getRoleIdList())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ROLE_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ROLE_ERROR.getMsg());
        }
        return sysUserService.insert(vo);
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "用户管理逻辑删除")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody List<Long> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.delete(vo);
    }

    @PostMapping("/revalidate")
    @ApiOperation(value = "用户管理复效")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.DELETE)
    public BaseResult revalidate(@RequestBody List<Long> vo){
        if (CollectionUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.revalidate(vo);
    }

    /**
     * 修改
     *
     * @param vo
     * @return
     */
    @PostMapping("/update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateUserRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.update(vo);
    }

    /**
     * 批量激活
     *
     * @param vo 入参
     * @return
     */
    @PostMapping("/activate")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.UPDATE)
    public BaseResult activate(@RequestBody List<Long> vo) {
        if (ObjectUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.activate(vo);
    }

    @PostMapping("/freeze")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.UPDATE)
    public BaseResult freeze(@RequestBody List<Long> vo) {
        if (ObjectUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.freeze(vo);
    }

    @PostMapping("/resetPassWd")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.USER,operateType = OperateTypeEnum.UPDATE)
    public BaseResult resetPassWd(@RequestBody ResetUserPassWord vo) {
        if (ObjectUtils.isEmpty(vo.getUserId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_USER_ID_ERROR.getCode(), GlobalResultStatus.PARAM_USER_ID_ERROR.getMsg());
        }
        return sysUserService.resetPassWd(vo);
    }

    @PostMapping("/selectPageByApp")
    @ApiOperation(value = "用户管理分页查询")
    public BaseResult selectPageByApp(@RequestBody SelectUserListByAppRequestVO vo) {
        if (StringUtils.isEmpty(vo.getPageIndex())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getMsg());
        }
        return sysUserService.selectPageByApp(vo);
    }

    @Deprecated
    @PostMapping("/selectPage")
    public BaseResult selectPage(@RequestBody SelectUserListRequestVO vo) {
        if (StringUtils.isEmpty(vo.getPageIndex())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getMsg());
        }
        return sysUserService.selectPage(vo);
    }

    //登录时查询当前用户信息，资源部分待修改
    @PostMapping("/getUserInfo")
    public BaseResult<UserInfoResVO> qryUserInfo(HttpServletRequest request) {
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        UserInfoResVO userInfoResVO = new UserInfoResVO();
        if (!ObjectUtils.isEmpty(xxlSsoUser)) {
            BeanUtils.copyProperties(xxlSsoUser, userInfoResVO);
            // 查询当前用户所拥有的资源列表
            //UserResourceInfoVO data = sysUserService.qryUserResourceTreeByUserId(Long.valueOf(xxlSsoUser.getUserid())).getData();
            UserResourceInfoVO data=new UserResourceInfoVO();
            data.setButtonList(new LinkedList<>());
            data.setMenuList(new LinkedList<>());
            userInfoResVO.setUserResourceInfo(data);
        }
        return BaseResult.buildSuccess(userInfoResVO);
    }

    @PostMapping("/selectResourceActionByUser")
    public BaseResult selectResourceActionByUser(@RequestBody SelectResourceActionListByUserReqVO vo){
        return sysUserService.selectResourceActionListByUser(vo);
    }

    @PostMapping("/authByUser")
    public BaseResult authByUser(@RequestBody AuthByUserReqVO authVO){
        return sysUserService.authByUser(authVO);
    };


}
